package com.sky.controller.user;


import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userDishController")
@RequestMapping("/user/dish")
public class DishController {
    @Autowired
    DishService dishService;

    @Autowired
    RedisTemplate redisTemplate;


    //根据分类id查询菜品
    @GetMapping("/list")
    public Result getDishList(String categoryId) {
        // 1.查询redis中的菜品
        List<DishVO> dishList = (List<DishVO>) redisTemplate.opsForValue().get("dish_" + categoryId);
        // 2.如果存在缓存
        if (dishList != null && dishList.size() > 0) {
            return Result.success(dishList);
        }
        // 3.没有缓存 从mysql中取出
        dishList = dishService.getDishFlavorList(categoryId);

        // 4. 将mysql中同步到redis中
        redisTemplate.opsForValue().set("dish_" + categoryId, dishList);
        return Result.success(dishList);
    }


}
